var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var circleUrl = require("url/circle");
var http = require("helper/http");

var isNoVoteUrl = circleUrl.url.base.isNoVote;
var setAllMemberNoVoteUrl = circleUrl.url.base.setAllMemberNoVote;
var setAllMemberCanVoteUrl = circleUrl.url.base.setAllMemberCanVote;
// UI
var headerAout = ui("headerAout");
var linearLayout_main = ui("linearLayout_main")

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
page.on("back", function() {
	app.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "设置圈内禁止发布投票",
})
header.refreshData();

/**
 * ********************************************************switch
 */
var addSwitch1 = linearLayout_main.add("switch1","source://view/reuse/form/mutiSwitch.ui");
var switch1 = ui("switch1");
var labelTitle = ui(addSwitch1 + ".labelTitle");
var aLayoutRoot = ui(addSwitch1 + ".aLayoutRoot");
switch1.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "全员禁止发布投票"
})
switch1.refreshData();
switch1.on("switchtag", function(data) {
	if (data == 0) {// 关闭，全员可发布投票
		setAllCanVote();
	}
	if (data == 1) {// 打开，全员不可发布投票、默认是打开
		setAllNoVote();
	}
});

var circleId = null;
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	circleId = page.getData().circleId;
	loadPage();
});

function loadPage() {
	//var swtxt = [ 0, " ", " " ]; //[开关状态 (0-关;1-开),关闭状态文字 ,开启状态文字]
	http.post(isNoVoteUrl, {id: circleId}, function(data) {
		if (data.isNoVote) {//全员禁止发布投票
			switch1.fire("switchzt", [ 1, " ", " " ]);
		}else{
			switch1.fire("switchzt", [ 0, " ", " " ]);
		}
	})
}
function setAllNoVote() {
	http.post(setAllMemberNoVoteUrl, {id: circleId}, function(data) {
	})
}
function setAllCanVote() {
	http.post(setAllMemberCanVoteUrl, {id: circleId}, function(data) {
	})
}
